/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.gap.employee;

import com.buanalintas.bynga.Action;
import com.buanalintas.bynga.Controller;
import com.buanalintas.bynga.Field;
import com.buanalintas.bynga.Logger;
import com.buanalintas.bynga.Page;
import com.buanalintas.bynga.Persistence;
import com.buanalintas.bynga.formatter.MoneyFormatter;
import com.gap.entities.GapMasterEmployee;
import com.gap.entities.GapMasterItem;
import com.gap.entities.GapTransactionEmployee;
import com.gap.entities.GapTransactionEmployeeDetail;
import java.math.BigDecimal;
import java.util.List;
import javax.persistence.EntityManager;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 *
 * @author Yunita
 */
public class TransactionEmployeeDetail extends Page {

    private Field posDetailId;
    private Field posDetailItemId;
    private Field posDetailPrice;
    private Field posDetailDiscount;
    private Field posDetailNett;
    private Field posDetailDescription;

    public TransactionEmployeeDetail() {
        super();

        setTheClass(GapTransactionEmployeeDetail.class);

        posDetailId = new Field();
        posDetailId.setName("posDetailId");
        posDetailId.setCaption("id");
        //posId.setInfo("pos id");
        posDetailId.setListProperties(" align=\"center\" width=\"1%\" nowrap");
        posDetailId.setInputProperties(" size=\"20\"");
        getFields().add(posDetailId);

        posDetailItemId = new Field();
        posDetailItemId.setName("posDetailItemId");
        posDetailItemId.setCaption("item");
        posDetailItemId.setRequired(true);
        posDetailItemId.setListProperties(" align=\"left\" width=\"15%\" nowrap");
        posDetailItemId.setInputProperties(" size=\"30\" ");
        getFields().add(posDetailItemId);

        posDetailDiscount = new Field();
        posDetailDiscount.setName("posDetailDiscount");
        posDetailDiscount.setCaption("discount percent");
        posDetailDiscount.setRequired(true);
//        posDetailDiscount.setDefaultValue(0);
//        posDiscount.setFormater(MoneyFormatter.getInstance());
        posDetailDiscount.setListProperties(" align=\"right\" width=\"10%\" nowrap");
        posDetailDiscount.setInputProperties(" size=\"20\" readonly");
        getFields().add(posDetailDiscount);

        posDetailPrice = new Field();
        posDetailPrice.setName("posDetailPrice");
        posDetailPrice.setCaption("price");
        posDetailPrice.setRequired(true);
        posDetailPrice.setDefaultValue(0);
        posDetailPrice.setFormater(MoneyFormatter.getInstance());
        posDetailPrice.setInputCase(Field.CASE_UPPER);
        posDetailPrice.setListProperties(" align=\"right\" width=\"10%\" nowrap");
        posDetailPrice.setInputProperties(" size=\"20\"");
        getFields().add(posDetailPrice);

        posDetailNett = new Field();
        posDetailNett.setName("posDetailNett");
        posDetailNett.setCaption("nett price");
        posDetailNett.setRequired(true);
        posDetailNett.setDefaultValue(0);
        posDetailNett.setFormater(MoneyFormatter.getInstance());
        posDetailNett.setInputCase(Field.CASE_UPPER);
        posDetailNett.setListProperties(" align=\"right\" width=\"10%\" nowrap");
        posDetailNett.setInputProperties(" size=\"20\"");
        getFields().add(posDetailNett);

        posDetailDescription = new Field();
        posDetailDescription.setName("posDetailDescription");
        posDetailDescription.setCaption(" description");
        //revisionDescription.setInfo("revision description");
        posDetailDescription.setListProperties(" width=\"20%\"");
        posDetailDescription.setInputProperties(" cols=\"50\" rows=\"5\"");
        posDetailDescription.setInputType(Field.INPUT_TEXT_AREA);
        getFields().add(posDetailDescription);

        fillFields();

        getFieldsAdd().clear();
        getFieldsAdd().add(posDetailItemId);
        getFieldsAdd().add(posDetailPrice);
        getFieldsAdd().add(posDetailDescription);

        getFieldsEdit().clear();
        getFieldsEdit().add(posDetailItemId);
        getFieldsEdit().add(posDetailPrice);
        getFieldsEdit().add(posDetailDescription);

        getFieldsList().clear();
        getFieldsList().add(posDetailItemId);
        getFieldsList().add(posDetailDiscount);
        getFieldsList().add(posDetailPrice);
        getFieldsList().add(posDetailNett);
        getFieldsList().add(posDetailDescription);

        getActions().add(Action.TRANS_DETAIL_LIST);
        getActions().add(Action.TRANS_DETAIL_ADD);
        getActions().add(Action.TRANS_DETAIL_EDIT);
        
        setController(new TransactionEmployeeDetailController(this));
    }
    
//    @Override
//    public void afterInit(HttpServletRequest request, HttpServletResponse response) {
//        EntityManager em = getEntityManager();
//            List<GapMasterEmployee> request;
//            request = em.createQuery("SELECT t FROM GapMasterEmployee t WHERE t.purchaseOrderDetailPurchaseOrderId.purchaseOrderId=" + ).setHint(Persistence.REFRESH_HINT, true).getResultList();
//            
////        String good = Condition.getGood(getEntityManager()).getConditionId();
//        posDetailItemId.setSelectorCondition("(t.itemUnitId.unitId='" + getCurrentUnit().getUnitId() + "' OR t.itemPublic='Y')");
//        saleDetailItemId.getSelectorAction().add(new String[]{"query", "saleDetailPrice", "", "SELECT t.itemPricePriceSell FROM IvMasterItemPrice t WHERE t.itemPriceUnitId.unitId='" + getCurrentUnit().getUnitId() + "' AND t.itemPriceItemId.itemId=:id"});
//        saleDetailItemId.getSelectorColumns().add(new String[]{"", "Stock", "align=\"right\" width=\"10%\"", "SELECT t.stockStock FROM IvMasterStock t WHERE t.stockConditionId.conditionId='" + good + "' AND t.stockUnitId.unitId='" + getCurrentUnit().getUnitId() + "' AND t.stockItemId.itemId=:itemId"});
//        saleDetailItemId.getSelectorColumns().add(new String[]{"", "Price", "align=\"right\" width=\"10%\"", "SELECT t.itemPricePriceSell FROM IvMasterItemPrice t WHERE t.itemPriceUnitId.unitId='" + getCurrentUnit().getUnitId() + "' AND t.itemPriceItemId.itemId=:itemId"});
//        posDetailDiscount.setDefaultValue();
//    }
    
    class TransactionEmployeeDetailController extends Controller {

        public TransactionEmployeeDetailController(Page page) {
            super(page);
        }

        @Override
        public void beforeInsert(Object data) {
            GapTransactionEmployeeDetail detail = (GapTransactionEmployeeDetail) data;
            EntityManager em = getEntityManager();
            BigDecimal total = BigDecimal.ZERO;

            
            GapMasterEmployee employee = null;
            //---employee---
            try {
                employee = (GapMasterEmployee) em.createQuery("SELECT t FROM GapMasterEmployee t WHERE t.employeeId='" + detail.getPosDetailPosId().getPosEmployeeId().getEmployeeId() + "'").setHint(Persistence.REFRESH_HINT, true).getSingleResult();
                System.out.println(" employee  ---->" + employee);
                
            } catch (Exception e) {
                System.out.println(" salahe employee  ---->" + e);
            }

            total = detail.getPosDetailPrice();
            BigDecimal percent = BigDecimal.ZERO;
            int item = 0;
            item = detail.getPosDetailItemId().getItemDiscount().intValue();
            try {
                if (item == 0) {
                    percent = total.multiply(employee.getEmployeeDiscount().divide(new BigDecimal(100)));
                    detail.setPosDetailDiscount(employee.getEmployeeDiscount());
                    System.out.println(" percent  1---->" + percent + " persen :"+detail.getPosDetailDiscount());
                } else {
                    percent = total.multiply(detail.getPosDetailItemId().getItemDiscount().divide(new BigDecimal(100)));
                    detail.setPosDetailDiscount(detail.getPosDetailItemId().getItemDiscount());
                    System.out.println(" percent  2---->" + percent +" dan discount :"+detail.getPosDetailDiscount());
                }
            } catch (Exception e) {
                Logger.getInstance().log(e);
                System.out.println(" salahe percent  ---->" + e);
            }
            total = total.subtract(percent);
            System.out.println(" total---->" + total);
            detail.setPosDetailNett(total);
        }

        @Override
        public void afterInsert(Object data) {
            super.afterInsert(data);
        }
        
        
    }
}
